package com.douwong.student.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.interfaces.ColorChangedListener;
import com.douwong.student.popuwindow.ColorPickerPopuwindow;
import com.douwong.student.utils.DateTools;
import com.douwong.student.utils.DensityUtil;
import com.douwong.student.utils.FileUtils;
import com.douwong.student.utils.ImageUtils;
import com.douwong.student.utils.ScreenUtils;
import com.douwong.student.view.PaletteView;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 手写板 小组讨论
 * @author leihuajie
 *
 */
public class HandWriteBoardActivity extends BaseActivity {
	private int lastColor;
	private int lastSize;
	private int width;
	private int height;
	private PaletteView paintView;
	private ImageView scale_img,commit_img;
	private LinearLayout tool_ll,easer_ll;
	private LinearLayout upload_ll, pen_ll, clear_ll, last_ll, next_ll, down_ll;
	private ColorPickerPopuwindow popu;
//	private WindowManager wm;
	private String content;
	private TextView name;
	private ColorChangedListener colorChange = new ColorChangedListener() {

		@Override
		public void sizeChanged(int size) {
			lastSize = size;
			paintView.setSize(lastSize);
		}

		@Override
		public void colorChanged(int color) {
			lastColor = color;
			paintView.setColor(lastColor);
		}
	};


	@Override
	protected void initView() {
		setContentView(R.layout.handwritten_layout);
		content=getIntent().getStringExtra("content");
		width = ScreenUtils.getScreenWidth();
		height = ScreenUtils.getScreenHeight();
		popu = new ColorPickerPopuwindow(HandWriteBoardActivity.this);
		commit_img = (ImageView) findViewById(R.id.commit);
		commit_img.setBackgroundResource(R.mipmap.save);
		commit_img.setVisibility(View.VISIBLE);
		paintView = (PaletteView) findViewById(R.id.paintview);
		paintView.isDraw = true;
		paintView.setPopuwindow(popu);
		tool_ll=(LinearLayout) findViewById(R.id.tool_ll);
		upload_ll = (LinearLayout) findViewById(R.id.upload_ll);
		upload_ll.setVisibility(View.GONE);
		clear_ll = (LinearLayout) findViewById(R.id.clear_ll);
		easer_ll= (LinearLayout) findViewById(R.id.easer_ll);
		pen_ll = (LinearLayout) findViewById(R.id.pen_ll);
		last_ll = (LinearLayout) findViewById(R.id.last_ll);
		next_ll = (LinearLayout) findViewById(R.id.next_ll);
		scale_img = (ImageView) findViewById(R.id.scale_img);
		name=(TextView) findViewById(R.id.name);
		lastColor = getResources().getColor(R.color.color_11);
		lastSize = 5;
		doPencil(paintView);
		HandWrittenOnClick listener = new HandWrittenOnClick();
		scale_img.setOnClickListener(listener);
		clear_ll.setOnClickListener(listener);
		pen_ll.setOnClickListener(listener);
		last_ll.setOnClickListener(listener);
		next_ll.setOnClickListener(listener);
		commit_img.setOnClickListener(listener);
		easer_ll.setOnClickListener(listener);
	}

	public void doPencil(View view) {
		paintView.setColor(lastColor);
		paintView.setSize(lastSize);
	}
	private class HandWrittenOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {

			if (popu.isShowing() && v.getId() != R.id.pen_ll) {
				popu.dismiss();
			}
			switch (v.getId()) {
				case R.id.easer_ll:
					paintView.isDraw=true;
					paintView.changePenMode(2);
					break;
			case R.id.clear_ll:
//				paintView.undo(Integer.MAX_VALUE);
				paintView.clear();
				break;
			case R.id.pen_ll:
				// mZoomListener.isDrag = false;
				paintView.isDraw = true;
				int[] location = new int[2];
				v.getLocationOnScreen(location);
				int x = (int) (location[0] - DensityUtil.dip2px(HandWriteBoardActivity.this, 500)
						- v.getWidth() * 1.3);
				if (!popu.isShowing()) {
					popu.showAtLocation(v, Gravity.NO_GRAVITY, x, location[1]);
					popu.setOnColorChangerLister(colorChange);
					popu.setOnDismissListener(new PopupWindow.OnDismissListener() {
						@Override
						public void onDismiss() {
							paintView.changePenMode(1);
						}
					});
				} else {
					popu.dismiss();
				}
				break;
			// 上一步
			case R.id.last_ll:
				paintView.undo(-1);
				break;
			// 下一步
			case R.id.next_ll:
				paintView.undo(1);
				break;
			// 保存图片
			case R.id.commit:
				tool_ll.setVisibility(View.GONE);
				commit_img.setVisibility(View.GONE);
				/*name.setVisibility(View.VISIBLE);
				name.setText(content);*/
				Bitmap tempBitmap = screenShot();
				String filePath = FileUtils.IMAGE_DIR + BaseApplication.sUserBean.getUserbean().getId() + DateTools.getDate1() + ".jpg";
				File file = new File(filePath);
				try {
					if (!file.exists()) {
						file.createNewFile();
					}
					tempBitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
					if (file.length() > 100 * 1024) {
						tempBitmap = ImageUtils.imageZoomLimtSize(tempBitmap, 100);
						tempBitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
					}
					Intent intent=new Intent();
					intent.putExtra("filePath", filePath);
					setResult(RESULT_OK, intent);
					finish();
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}

		}
	}
	//截图
	private Bitmap screenShot() {
		View view = getWindow().getDecorView();
		if (view == null) {
			return null;
		}
		view.layout(0, 0, width, height);
		view.setDrawingCacheEnabled(true);
		// Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
		return view.getDrawingCache();
	}
}
